import 'package:flutter/material.dart';
import 'package:flutter_widgets_pc/widgets/button/kq_ink_well.dart';

import '../../resources/kq_theme_colors.dart';
import '../checkbox/round_checkbox.dart';
import '../divider/kq_divider.dart';

class KqChooseItemView extends StatefulWidget {
  final String? title;
  final bool isChecked;
  final CheckBoxDirection direction;
  final Function(bool checked)? onItemTap;
  final GestureTapUpCallback? onSecondaryTap;
  final bool disabled;

  /// 是否build时自动设置_checked=isChecked
  final bool updateCheckedWhenBuild;

  /// 自定义右侧控件
  final Widget? customRightView;

  /// 自定义左侧控件，在checkbox和文字之间
  final Widget? customLeftView;

  /// 背景颜色
  final Color? bgColor;

  final TextStyle? textStyle;

  /// 垂直padding
  final double? paddingVertical;

  final double? paddingHorizontal;

  /// checkbox和text之间的距离，默认16
  final double? textLeftPadding;

  const KqChooseItemView({
    super.key,
    this.isChecked = false,
    this.title,
    this.onItemTap,
    this.onSecondaryTap,
    this.direction = CheckBoxDirection.left,
    this.disabled = false,
    this.bgColor,
    this.customLeftView,
    this.textStyle,
    this.paddingHorizontal,
    this.paddingVertical,
    this.customRightView,
    this.textLeftPadding,
    this.updateCheckedWhenBuild = false,
  });

  @override
  State<StatefulWidget> createState() => _KqChooseItemViewState();
}

class _KqChooseItemViewState extends State<KqChooseItemView> {
  bool _isChecked = false;

  @override
  void initState() {
    super.initState();
    _isChecked = widget.isChecked;
  }

  @override
  Widget build(BuildContext context) {
    if (widget.updateCheckedWhenBuild) {
      _isChecked = widget.isChecked;
    }
    return Column(
      children: [
        KqInkWell(
          onTap: () {
            if (!widget.disabled) {
              _isChecked = !_isChecked;
              widget.onItemTap?.call(_isChecked);
              setState(() {});
            }
          },
          onSecondaryTap: widget.onSecondaryTap,
          horizontalPadding: widget.paddingHorizontal ?? 15,
          verticalPadding: widget.paddingVertical ?? 14,
          backgroundColor: widget.bgColor ?? KqThemeColors.bgWhite,
          child: Row(
            children: [
              Visibility(
                  visible: widget.direction == CheckBoxDirection.left,
                  child: IgnorePointer(
                      child: RoundCheckBox(
                    isChecked: _isChecked,
                    disabled: widget.disabled,
                    onTap: (bool? check) {},
                  ))),
              if (widget.customLeftView != null) widget.customLeftView!,
              Expanded(
                  child: Container(
                      padding: EdgeInsets.only(
                          left: widget.direction == CheckBoxDirection.left
                              ? widget.textLeftPadding ?? 16
                              : 0),
                      child: Text(
                        widget.title ?? '',
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: widget.textStyle ??
                            const TextStyle(
                                fontSize: 16, color: KqThemeColors.text26),
                      ))),
              if (widget.customRightView != null) widget.customRightView!,
              Visibility(
                  visible: widget.direction != CheckBoxDirection.left,
                  child: IgnorePointer(
                      child: RoundCheckBox(
                    isChecked: _isChecked,
                    disabled: widget.disabled,
                    onTap: (bool? check) {},
                  )))
            ],
          ),
        ),
        const KqDivider(),
      ],
    );
  }
}

enum CheckBoxDirection { left, right }
